package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * http://www.sohu.com/ 搜狐网
 * @author allen
 */
public class SohuNewsAnalyse implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://.*.sohu.com/[0-9]{8}/n[0-9]+[_0-9]*.shtml",
				"http://.*.stock.sohu.com/news/cn/[0-9]+/[0-9]+/[0-9]+.shtml"
				};
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}

		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select("#channel-nav,#nav,.content-footer,.ad675,.new_hot1,#tagHotg,#SOHUCS,.rightbar-wrapper,[align=center] a").remove();
		doc.select(".section-newslist-w").remove();
		doc.select(".mutu").remove();
		doc.select(".lottery-box").remove();
		doc.select("[style*=display:none;]").remove();

			title =doc.select("div.content-box > h1").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("div#contentA > div.left > h1").text();
				if (StringUtils.isBlank(title)) {
					title=doc.select("title").text();
					title=StringUtils.substringBefore(title, "-");
				}
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div.articleBody"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div#contentText"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div#textcont"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.article"));
						if(StringUtils.isBlank(content)){
							doc.select("select[name=gotopage]").remove();
							content=HtmlCleaner.getContentHtml(url,doc.select("td#fontzoom"));
						}
					}
				}
			}

			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div[itemprop=articleBody]"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.news_content"));
			}
			String tempdate=doc.select("div#container.area > div.content-wrapper > div.content-box > div.time-fun > div.time-source > div.time").text();
			if(tempdate==null || tempdate.trim().length()==0){
				tempdate=doc.select("div#contentA.area > div.left > div.timeFun > div.l > div.time").text();
				if (StringUtils.isBlank(tempdate)) {
					tempdate=doc.select("span.time").text();
					if (StringUtils.isBlank(tempdate)) {
						tempdate=doc.select(".date,h3,td.pd3").text();
					}
				}
			}
			date=DateUtils.matchDate(tempdate);
			if (date==null){
				date=JSoupUtils.matchDate(doc, "来源：");
			}
			String author=JSoupUtils.matchAuthor(doc, "来源：");

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
	     news.setAuthor(author);

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		SohuNewsAnalyse a=new SohuNewsAnalyse();
		String url="http://q.stock.sohu.com/news/cn/446/600446/3436981.shtml";
		System.err.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
